package com.neusoft.elm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.impl.CartServiceImpl;

public class CartController {
    
    // 获取购物车列表
    public Object listCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("=== 获取购物车列表 ===");
        
        Cart cart = new Cart();
        if (request.getParameter("businessId") != null) {
            cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
            System.out.println("商家ID: " + cart.getBusinessId());
        }
        if (request.getParameter("userId") != null) {
            cart.setUserId(request.getParameter("userId"));
            System.out.println("用户ID: " + cart.getUserId());
        }
        
        CartService service = new CartServiceImpl();
        List<Cart> cartResult = service.listCart(cart);
        
        System.out.println("购物车商品数量: " + cartResult.size());
        return cartResult;
    }

    // 添加到购物车
    public Object saveCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("=== 添加到购物车 ===");
        
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        cart.setQuantity(1); // 默认数量为1
        
        System.out.println("食品ID: " + cart.getFoodId());
        System.out.println("商家ID: " + cart.getBusinessId());
        System.out.println("用户ID: " + cart.getUserId());
        
        CartService service = new CartServiceImpl();
        int result = service.saveCart(cart);
        
        System.out.println("添加结果: " + result);
        return result;
    }

    // 从购物车移除
    public Object removeCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("=== 从购物车移除 ===");
        
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        
        CartService service = new CartServiceImpl();
        int result = service.removeCart(cart);
        
        System.out.println("移除结果: " + result);
        return result;
    }

    // 更新购物车数量
    public Object updateCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("=== 更新购物车数量 ===");
        
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
        
        System.out.println("新数量: " + cart.getQuantity());
        
        CartService service = new CartServiceImpl();
        int result = service.updateCart(cart);
        
        System.out.println("更新结果: " + result);
        return result;
    }
}